package com.zh.mp.demo.aspect;

import com.zh.mp.demo.annotation.Log;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

@Aspect
@Component
public class AnnotationAop {

    @Pointcut(value = "@annotation(log)", argNames = "log")
    public void pointcut(Log log) {
    }

    @Around(value = "pointcut(log)", argNames = "joinPoint, log")
    public Object around(ProceedingJoinPoint joinPoint, Log log) throws Throwable {
        try {
            System.out.println("around");
            Object proceed = joinPoint.proceed();
            System.out.println("proceed "+proceed);
            return proceed;
        } catch (Throwable throwable) {
            throw throwable;
        }
    }

    //@Before("@annotation(com.zh.mp.demo.annotation.Log)")
    @Before(value = "pointcut(log)", argNames = "joinPoint, log")
    public void before(JoinPoint joinPoint, Log log) {
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
//        Method method = signature.getMethod();
//        Log log2 = method.getAnnotation(Log.class);
//        System.out.println("注解式拦截 " + log2);
        System.out.println("注解式拦截 " + log);
    }
}